package com.study.talk.ui.app.home

import android.app.Application
import androidx.lifecycle.viewModelScope
import com.study.common.EventBusUtil
import com.study.common.GSONUtil
import com.study.common.annotation.Desc
import com.study.talk.ui.app.base.BaseViewModel
import com.study.talk.data.model.GroupMessageRecord
import com.study.talk.data.model.MessageRecord
import com.study.talk.netty.message.MessageType
import com.study.talk.netty.message.TalkMessage
import com.study.talk.netty.message.code.MessageCode
import com.study.talk.netty.message.response.GroupMessageResponse
import com.study.talk.netty.message.response.GroupMessageSelfResponse
import com.study.talk.netty.message.response.MessageResponse
import com.study.talk.netty.message.response.MessageSelfResponse
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(application: Application) : BaseViewModel(application) {

    @Desc("第一次登录从服务器加载缓存数据")
    fun loadAllMsg() {
        viewModelScope.launch {
            val result = msgRepository.getAllMsg()
            if (result.isNotEmpty()) {
                val messages = mutableListOf<TalkMessage>()
                result.mapTo(messages) { msg ->
                    val type = MessageCode.getMessageByMessageType(msg.type.toByte())
                    GSONUtil.fromJson(msg.message, type)
                }
                withContext(Dispatchers.Main) {
                    messages.forEach {
                        when (it.type) {
                            MessageType.GROUP_MESSAGE_SELF_RESPONSE -> {
                                msgRepository.addMsg(it as GroupMessageSelfResponse, app.userInfo)
                            }

                            MessageType.MESSAGE_SELF_RESPONSE -> {
                                msgRepository.addMsg(it as MessageSelfResponse, app.userInfo)
                            }

                            MessageType.SEND_MESSAGE_RESPONSE -> {
                                val msg =
                                    MessageRecord(
                                        it as MessageResponse, false, true
                                    )
                                msgRepository.addMsg(msg)
                            }

                            MessageType.GROUP_MESSAGE_RESPONSE -> {
                                val msg =
                                    GroupMessageRecord(
                                        it as GroupMessageResponse, false, true
                                    )
                                msgRepository.addMsg(msg)
                            }
                        }
                    }
                }
            }
        }
    }

    @Desc("加载server中未读的消息")
    fun loadCacheMsg() {
        viewModelScope.launch {
            val result = msgRepository.getServerCacheMsg(app.localUserId)
            if (result.isNotEmpty()) {
                val messages = mutableListOf<TalkMessage>()
                result.mapTo(messages) { msg ->
                    val type = MessageCode.getMessageByMessageType(msg.type.toByte())
                    GSONUtil.fromJson(msg.message, type)
                }
                messages.forEach {
                    EventBusUtil.sendEvent(it)
                }
            }
        }
    }
}